import 'package:flutter/material.dart';

import 'demo1/user_info_entity.dart';
import 'demo1/my_sqlite.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MySqlite.forFeature();
  runApp(DBTest());
}

class DBTest extends StatefulWidget {
  const DBTest({super.key});

  @override
  State<DBTest> createState() => _TestState();
}

class _TestState extends State<DBTest> {
  @override
  void initState() {
    super.initState();

    try {
      UserInfoEntity userInfoEntity = UserInfoEntity.instance();
      userInfoEntity.find();
    } catch (err) {
      //注意由于打开数据库属于异步操作, 虽然UserInfoEntity已经实例化了, 但是数据库的初始化并没有完成
      print("报错了: $err");
    }
  }

  Widget buildColumn() =>
      // #docregion Column
      Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          TextButton(
            onPressed: () => UserInfoEntity.instance().insert({
              "name": "刘小明",
              "sex": 1,
              "phone": "123456789",
            }),
            child: const Text("insert"),
          ),
          TextButton(
            onPressed: () async {
              var result = await UserInfoEntity.instance().find();
              print(result);
            },
            child: Text("find"),
          ),
          TextButton(
            onPressed: () async {
              var result = await UserInfoEntity.instance().remove({"id": 1});
              print(result);
            },
            child: Text("remove"),
          ),
        ],
      );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter layout demo',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter layout demo'),
        ),
        // Change to buildColumn() for the other column example
        body: Center(child: buildColumn()),
      ),
    );
  }
}
